﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;

namespace DragonSolution.Web.Common.Common
{
    public class ImageUtil
    {
        /// <summary>
        /// 获取图片文件二进制
        /// </summary>
        /// <param name="fileName">文件物理路径</param>
        /// <returns></returns>
        public static byte[] GetImageFileBytes(string fileName)
        {
            byte[] result = null;
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                result = new byte[fs.Length];
                fs.Read(result, 0, (int)fs.Length);
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// 流转换成二进制
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToByte(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);

            return bytes;
        }

        /// <summary>
        /// 二进制转换流
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Stream ByteToSteam(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }

        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="fileBytes"></param>
        /// <returns></returns>
        public static bool CompressImages(byte[] fileBytes, string savePath)
        {
            Stream fs = null;
            Image iamgeSource = null;
            try
            {
                fs = ByteToSteam(fileBytes);
                iamgeSource = Image.FromStream(fs);
                ImageFormat tFormat = iamgeSource.RawFormat;

                EncoderParameters ep = new EncoderParameters();
                long[] qy = new long[1];
                qy[0] = 20;//设置压缩的比例1-100  
                EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
                ep.Param[0] = eParam;

                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }

                if (jpegICIinfo != null)
                {
                    iamgeSource.Save(savePath, jpegICIinfo, ep);
                }
                else
                {
                    iamgeSource.Save(savePath, tFormat);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                fs.Close();
                iamgeSource.Dispose();
            }

        }
    }
}
